package ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.SignContractListBean;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.presenter.SignContractPresenter;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.SignContractCallBack;

import ui.fragment.StepOneFragment;
import ui.fragment.StepThreeFragment;
import ui.fragment.StepTwoFragment;

public class SignStepActivity extends BaseActivity<SignContractPresenter> implements SignContractCallBack {


    private int customersId;
    private StepOneFragment stepOneFragment;
    private StepThreeFragment stepThreeFragment;
    private StepTwoFragment stepTwoFragment;
    private int performanceId;
    public int signType;

    @Override
    protected SignContractPresenter createPresenter() {
        return new SignContractPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_sign_step;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("签约详情");
    }

    @Override
    public void initData() {
        super.initData();
        customersId = getIntent().getIntExtra("customersId", 0);
        performanceId = getIntent().getIntExtra("performanceId", 0);
        signType = getIntent().getIntExtra("type", 0);
        if (signType == 1) {  //右上角添加
            String signId = SPUtils.getInstance(this).getString("performanceId", "");
            if (signId.equals("") || null == signId) {  //没有数据代表没添加过  默认从第一步开始
                initFragment1();
            } else {
                performanceId = Integer.parseInt(signId);
                mPresenter.getSingnContractdetail(performanceId);
            }
        } else {
            int step = getIntent().getIntExtra("step", 0);

            switch (step) {
                case 1:
                    initFragment1();
                    break;
                case 2:
                    initFragment2();
                    break;
                case 3:
                    initFragment3();
                    break;
            }
        }
    }

    //第一个fr 点击下一步
    public void oneNext() {
        initFragment2();
    }

    //第2个fr 点击下一步
    public void twoNext() {
        initFragment2();
    }

    //第3个fr 点击下一步
    public void threeNext() {

    }

    public void initFragment1() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (stepOneFragment == null) {
            stepOneFragment = StepOneFragment.newInstance(customersId, performanceId, signType);
            transaction.add(R.id.framlayout, stepOneFragment);
        }

        hideFragment(transaction);
        transaction.show(stepOneFragment);
        transaction.commit();
    }


    public void initFragment2() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (stepTwoFragment == null) {
            stepTwoFragment = StepTwoFragment.newInstance(customersId, performanceId, signType);
            transaction.add(R.id.framlayout, stepTwoFragment);
        }
        hideFragment(transaction);
        transaction.show(stepTwoFragment);
        transaction.commit();

    }

    public void initFragment3() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (stepThreeFragment == null) {
            stepThreeFragment = StepThreeFragment.newInstance(customersId, performanceId, signType);
            transaction.add(R.id.framlayout, stepThreeFragment);
        }
        hideFragment(transaction);
        transaction.show(stepThreeFragment);
        transaction.commit();

    }

    private void hideFragment(FragmentTransaction transaction) {
        if (stepOneFragment != null) {
            transaction.hide(stepOneFragment);
        }
        if (stepTwoFragment != null) {
            transaction.hide(stepTwoFragment);
        }
        if (stepThreeFragment != null) {
            transaction.hide(stepThreeFragment);
        }
    }

    @Override
    public void getSifnContractListSuccess(SignContractListBean signContractListBean) {

    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        if (signContractDetailBean.getStep() == 1) {
            initFragment1();
        } else if (signContractDetailBean.getStep() == 2) {
            initFragment2();
        } else if (signContractDetailBean.getStep() == 3) {
            initFragment3();

        } else {

        }
    }
}